package cn.kicsy.www.anonymous_bbs.config;

import cn.kicsy.www.anonymous_bbs.entity.Post;
import cn.kicsy.www.anonymous_bbs.entity.Comment;
import cn.kicsy.www.anonymous_bbs.repository.PostRepository;
import cn.kicsy.www.anonymous_bbs.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Random;

@Component
public class DataInitializer implements ApplicationRunner {
    
    @Autowired
    private PostRepository postRepository;
    
    @Autowired
    private CommentRepository commentRepository;
    
    private final Random random = new Random();
    
    private static final String[] RANDOM_NICKNAMES = {
        "快乐的猫咪1234", "神秘的狗狗5678", "勇敢的兔子9012", "聪明的老虎3456", "善良的狮子7890",
        "可爱的熊猫2345", "优雅的企鹅6789", "强大的海豚0123", "温柔的蝴蝶4567", "幽默的蜜蜂8901"
    };
    
    private static final String[] POST_CONTENTS = {
        "今天天气真好，出来逛逛论坛。大家都在讨论什么呢？",
        "有人知道怎么解决这个技术问题吗？我遇到了一个棘手的bug。",
        "分享一个有趣的小故事，希望大家喜欢。",
        "这个论坛的氛围真不错，大家都很友好。",
        "刚刚发现了一个很酷的功能，想和大家分享一下使用心得。",
        "有没有人推荐一些好看的电影或者书籍？最近有点无聊。",
        "今天学习了一个新技能，感觉很有收获！",
        "来讨论一下这个热门话题，你们怎么看？",
        "分享一些实用的生活小技巧，希望对大家有帮助。",
        "第一次来这个论坛，大家好啊！"
    };
    
    private static final String[] COMMENT_CONTENTS = {
        "同意你的观点，我也有同样的感受。",
        "这个问题我之前也遇到过，可以试试这个方法...",
        "谢谢分享，学到了！",
        "哈哈哈，太有趣了！",
        "我有不同的看法，我觉得...",
        "期待更多的分享！",
        "这个建议很不错，我会试试看。",
        "说得对极了！",
        "受益匪浅，感谢分享。",
        "这个话题很有深度，值得深思。"
    };

    private static final String[] IP_ADDRESSES = {
        "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5",
        "192.168.1.6", "192.168.1.7", "192.168.1.8", "192.168.1.9", "192.168.1.10"
    };
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 暂时禁用数据初始化，以便应用程序能够正常启动
        System.out.println("数据初始化已禁用");
        // 检查是否已有数据，如果没有则初始化
        // if (postRepository.count() == 0) {
        //     initializeData();
        // }
    }

    private void initializeData() {
        // 创建10个示例帖子
        for (int i = 0; i < 10; i++) {
            Post post = new Post();
            post.setAuthorName(RANDOM_NICKNAMES[random.nextInt(RANDOM_NICKNAMES.length)]);
            post.setContent(POST_CONTENTS[i]);
            post.setIpAddress(IP_ADDRESSES[random.nextInt(IP_ADDRESSES.length)]);
            post.setCreatedAt(LocalDateTime.now().minus(random.nextInt(30), ChronoUnit.DAYS));
            post.setViewCount(random.nextInt(1000));
            post.setLikeCount(random.nextInt(100));
            
            // 保存帖子
            Post savedPost = postRepository.save(post);
            
            // 为每个帖子创建2-5条评论
            int commentCount = 2 + random.nextInt(4);
            for (int j = 0; j < commentCount; j++) {
                Comment comment = new Comment();
                comment.setAuthorName(RANDOM_NICKNAMES[random.nextInt(RANDOM_NICKNAMES.length)]);
                comment.setContent(COMMENT_CONTENTS[random.nextInt(COMMENT_CONTENTS.length)]);
                comment.setIpAddress(IP_ADDRESSES[random.nextInt(IP_ADDRESSES.length)]);
                comment.setPost(savedPost);
                comment.setCreatedAt(savedPost.getCreatedAt().plus(random.nextInt(3), ChronoUnit.HOURS));
                comment.setLikeCount(random.nextInt(20));
                
                // 保存评论
                Comment savedComment = commentRepository.save(comment);

                // 为部分评论添加回复
                if (random.nextBoolean()) {
                    int replyCount = 1 + random.nextInt(3);
                    for (int k = 0; k < replyCount; k++) {
                        Comment reply = new Comment();
                        reply.setAuthorName(RANDOM_NICKNAMES[random.nextInt(RANDOM_NICKNAMES.length)]);
                        reply.setContent(COMMENT_CONTENTS[random.nextInt(COMMENT_CONTENTS.length)]);
                        reply.setIpAddress(IP_ADDRESSES[random.nextInt(IP_ADDRESSES.length)]);
                        reply.setPost(savedPost);
                        reply.setParentComment(savedComment);
                        reply.setCreatedAt(savedComment.getCreatedAt().plus(random.nextInt(2), ChronoUnit.HOURS));
                        reply.setLikeCount(random.nextInt(10));
                        
                        // 保存回复
                        commentRepository.save(reply);
                    }
                }
            }
        }
        
        System.out.println("示例数据初始化完成！");
    }
}